package com.clever.mall.service.impl;

import com.clever.mall.commons.dto.ShoppingCartDto;
import com.clever.mall.commons.exception.ServiceException;
import com.clever.mall.commons.result.BaseResult;
import com.clever.mall.entity.Product;
import com.clever.mall.entity.ShoppingCart;
import com.clever.mall.mapper.ProductMapper;
import com.clever.mall.mapper.ShoppingCartMapper;
import com.clever.mall.service.ShoppingCartService;
import com.clever.mall.utils.IsClassNull;
import com.clever.mall.utils.MyBeanUtils;
import com.clever.mall.utils.StatusCode;
import com.clever.mall.vo.ShoppingCartVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @program: project-item
 * @author: hzx
 * @since: JDK 1.8
 * @create: 2020-11-10 09:39
 **/
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    //默认单页数量
    public static final int DEFAULT_SIZE = 10;

    //购物车
    @Resource
    private ShoppingCartMapper mapper;

    //产品
    @Resource
    private ProductMapper pMapper;

    /**
     * 通过用户id查询所有购物车内容
     * @param uid
     * @return
     */
    @Override
    public List<ShoppingCartVo> selectShoppingCartAll(int uid) {
        List<ShoppingCart> shoppingCarts = mapper.selectShoppingCartAll(uid);
        if (shoppingCarts == null) {
            throw new ServiceException(StatusCode.NO_DATA_ERROR);
        }
        //查询
        List<ShoppingCartVo> shoppingCartVos = MyBeanUtils.copyListProperties(shoppingCarts, ShoppingCartVo::new);
        return shoppingCartVos;
    }

    /**
     * 分页查询,第一页为 1
     * @param uid
     * @param page  第几页
     * @param size  单页数量
     * @return
     */
    @Override
    public List<ShoppingCartVo> selectShoppingCartByLimit(int uid, int page, Integer size) {
        if (uid < 1 || page < 1) {
            throw new ServiceException(StatusCode.DATA_FORMAT_ERROR);
        }
        size = (size<1 || size==null) ? DEFAULT_SIZE : size;
        int tail = page * size;
        //查询
        List<ShoppingCart> shoppingCarts = mapper.selectShoppingCartByLimit(uid, tail - size, tail);
        if (shoppingCarts == null) {
            throw new ServiceException(StatusCode.NO_DATA_ERROR);
        }
        List<ShoppingCartVo> shoppingCartVos = MyBeanUtils.copyListProperties(shoppingCarts, ShoppingCartVo::new);
        return shoppingCartVos;
    }

    /**
     * 将商品加入购物车
     * @param shoppingCartDto
     * @return
     */
    @Override
    public int insertShoppingCart(ShoppingCartDto shoppingCartDto) {
        if (shoppingCartDto == null) {
            throw new ServiceException(StatusCode.NO_DATA_ERROR);
        }
        if (IsClassNull.isNotEmptyBean(shoppingCartDto)) {
            throw new ServiceException(StatusCode.DATA_FORMAT_ERROR);
        }
        //数据验证
        Product product = pMapper.selectById(shoppingCartDto.getProductId());
        //产品是否存在
        if (product == null) {
            throw new ServiceException(StatusCode.PRODUCT_NOT_ERROR);
        }
        //价格验证
        if (shoppingCartDto.getPrice().compareTo(product.getPromotionPrice()) != 0) {
            throw new ServiceException(StatusCode.PRICE_ERROR);
        }
        //库存验证
        if (shoppingCartDto.getNumber() > product.getSum()) {
            throw new ServiceException(StatusCode.STOCKPILE_ERROR);
        }
        //卖家id验证
        if (!product.getUserId().equals(shoppingCartDto.getSellerId())) {
            throw new ServiceException(StatusCode.SELLER_DATA_ERROR);
        }

        //判断购物车是否已经存在该产品，存在则添加数量
        int insertShoppingCart = 0;
        ShoppingCart shoppingCartIF = mapper.selectShoppingCartByUserIdAndProjectId(
                shoppingCartDto.getUserId(), shoppingCartDto.getProductId());
        if (shoppingCartIF == null) {
            //录入数据库
            ShoppingCart shoppingCart = new ShoppingCart();
            MyBeanUtils.copyProperties(shoppingCartDto,shoppingCart);
            shoppingCart.setCreateTime(new Date());
            shoppingCart.setState("0");
            insertShoppingCart = mapper.insertShoppingCart(shoppingCart);
        } else {
            //库存验证
            if (shoppingCartDto.getNumber() + shoppingCartIF.getNumber() > product.getSum()) {
                throw new ServiceException(StatusCode.STOCKPILE_ERROR);
            }
            //修改数量
            insertShoppingCart = mapper.updateShoppingCartNumber(
                    (shoppingCartDto.getNumber() + shoppingCartIF.getNumber()),shoppingCartIF.getScId());
        }

        //影响行数判定
        if (insertShoppingCart < 1) {
            throw new ServiceException(StatusCode.ERROR);
        }
        return insertShoppingCart;
    }

    /**
     * 通过用户id和产品id删除购物车内容
     * @param uid
     * @param pid
     * @return
     */
    @Override
    public int deleteShoppingCart(int uid, int pid) {
        if (uid < 1 || pid < 1) {
            throw new ServiceException(StatusCode.DATA_FORMAT_ERROR);
        }
        //删除
        int deleteShoppingCart = mapper.deleteShoppingCart(uid, pid);
        if (deleteShoppingCart != 1) {
            throw new ServiceException(StatusCode.ERROR);
        }
        return deleteShoppingCart;
    }

    /**
     * 批量删除
     * @param shoppingCartDtoList
     * @return
     */
    @Override
    public int deleteShoppingCartList(List<ShoppingCartDto> shoppingCartDtoList) {
        for (ShoppingCartDto scDto :
                shoppingCartDtoList) {
            if (scDto.getUserId() < 1 || scDto.getProductId() < 1) {
                throw new ServiceException(StatusCode.DATA_FORMAT_ERROR);
            }
        }

        //复制数据
        List<ShoppingCart> shoppingCartList = MyBeanUtils.copyListProperties(shoppingCartDtoList, ShoppingCart::new);
        //删除
        int deleteShoppingCart = mapper.deleteShoppingCartList(shoppingCartList);
        if (deleteShoppingCart < 1) {
            throw new ServiceException(StatusCode.ERROR);
        }
        return deleteShoppingCart;
    }
}
